package cn.wolfcode.service.impl;

import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.Product;
import cn.wolfcode.domain.SeckillProduct;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.feign.ProductFeignApi;
import cn.wolfcode.mapper.SeckillProductMapper;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.ISeckillProductService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class SeckillProductServiceImpl implements ISeckillProductService {
    @Autowired
    private SeckillProductMapper seckillProductMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private ProductFeignApi productFeignApi;

    @Override
    public List<SeckillProductVo> selectByTime(int time) {
        //查询秒杀商品服务
        List<SeckillProduct> seckillProducts = seckillProductMapper.queryCurrentlySeckillProduct(time);
        //把商品id放到list集合中
        List<Long> ids = new ArrayList<>();
        for (SeckillProduct seckillProduct : seckillProducts) {
            ids.add(seckillProduct.getProductId());
        }
        //远程调用商品服务查询商品数据
        List<SeckillProductVo> vos = new ArrayList<>();
        Result<List<Product>> results = productFeignApi.queryByIds(ids);
        if(results != null && !results.hasError()) {
            List<Product> productList = results.getData();
            System.out.println(productList);
            Map<Long, Product> map = new HashMap<>();
            //进行聚合操作，最终返回一个vo集合对象
            for (Product product : productList) {
                map.put(product.getId(), product);
            }
            for (SeckillProduct seckillProduct : seckillProducts) {
                SeckillProductVo vo = new SeckillProductVo();
                Product product = map.get(seckillProduct.getProductId());
                BeanUtils.copyProperties(product, vo);
                BeanUtils.copyProperties(seckillProduct, vo);
                vos.add(vo);
            }
        }
        return vos;
    }

    @Override
    public List<SeckillProductVo> queryByTime(int time) {
        //查询redis中的所有vo对象
        List<Object> values = redisTemplate.opsForHash().values(SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time + ""));
        List<SeckillProductVo> vos = new ArrayList<>();
        for (Object value : values) {
            vos.add(JSON.parseObject(value + "",SeckillProductVo.class));
        }
        return vos;
    }

    @Override
    public SeckillProductVo find(int time, Long seckillId) {
        Object obj_json = redisTemplate.opsForHash().get(SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time + ""), seckillId + "");
        return JSON.parseObject(obj_json + "",SeckillProductVo.class);
    }

    @Override
    public void syncRedisStockCount(Integer time, Long seckillId) {
        //查询真实库存
        int count = seckillProductMapper.getStockCount(seckillId);
        //把库存重新更新到redis中
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_STOCK_COUNT_HASH.getRealKey(time + ""),
                seckillId+"",count+"");
        log.info("数据同步成功");
    }
}
